package com.letoy.edu.service.Impl;

import com.letoy.edu.dao.StudentInfoMapper;
import com.letoy.edu.entity.StudentInfo;
import com.letoy.edu.service.StudentInfoService;
import com.letoy.edu.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class StudentInfoServiceImpl implements StudentInfoService {


    @Autowired
    StudentInfoMapper studentInfoMapper;

    @Cacheable(value = "StudentInfoAll", key = "'student'")
    @Override
    public HashMap<String, Object> findAllStudent() {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<StudentInfo> allStudent = studentInfoMapper.findAllStudent();
        if (allStudent.isEmpty()) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", allStudent);
        return hashMap;
    }

    @Cacheable(value = "StudentInfoByNumber", key = "'student_'+ #number")
    @Override
    public HashMap<String, Object> findStudentByNumber(int number) {
        HashMap<String, Object> hashMap = new HashMap<>();
        Student studentByNumber = studentInfoMapper.findStudentByNumber(number);
        if (studentByNumber == null) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }

        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", studentByNumber);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> insertStudent(StudentInfo studentInfo) {
        HashMap<String, Object> hashMap = new HashMap<>();

        int i = studentInfoMapper.insertStudent(studentInfo);
        if (i == 0) {
            hashMap.put("status", "1");
            hashMap.put("msg", "新增失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "新增成功");
        return hashMap;
    }

    @Override
    public HashMap<String, Object> updateStudent(StudentInfo studentInfo) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int i = studentInfoMapper.updateStudent(studentInfo);
        if (i == 0) {
            hashMap.put("status", "1");
            hashMap.put("msg", "更新失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "更新成功");
        return hashMap;
    }

    @Override
    public HashMap<String, Object> deleteStudent(int number) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int i = studentInfoMapper.deleteStudent(number);
        if (i == 0) {
            hashMap.put("status", "1");
            hashMap.put("msg", "删除失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "删除成功");
        return hashMap;
    }
}
